//
//  HomeViewController.m
//  tuanGG
//
//  Created by 微他 on 15/9/25.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "HomeViewController.h"
#import "Header.h"
#import "LoginViewController.h"
#import "RegisteredViewController.h"
@interface HomeViewController ()<UIScrollViewDelegate>
{
    NSInteger _currentIndex;
}
@property (nonatomic,strong)UIScrollView * scrollView;
@property (nonatomic,strong)NSMutableArray * dataSources;
@property (nonatomic,strong)NSMutableArray * imageArray;
//登陆与注册按钮
@property (nonatomic,strong)UIButton * loginBtn;

@property (nonatomic,strong)UIPageControl * pageControl;

@property (nonatomic,strong)UIImageView * firstImage;
@property (nonatomic,strong)UIImageView * SecondImage;
@property (nonatomic,strong)UIImageView * ThredImage;
@property (nonatomic,strong)UILabel * firstLabel;
@property (nonatomic,strong)UILabel * secondLabel;
@property (nonatomic,strong)UILabel * thredLabel;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self liazeInterUserFace];
}

#pragma mark - <数据初始化>
- (NSMutableArray *)dataSources
{
    if (!_dataSources) {
        _dataSources = [[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg", nil];
    }
    return _dataSources;
}

- (NSMutableArray *)imageArray
{
    if (!_imageArray) {
        
        _imageArray = [[NSMutableArray alloc]init];
    }
    return _imageArray;
}

#pragma mark - <界面初始化>
- (void)liazeInterUserFace
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    _scrollView.backgroundColor = [UIColor grayColor];
    _scrollView.center = CGPointMake(ScreenWidth/2,ScreenHeight/2);
    _scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds)*3, CGRectGetHeight(self.view.bounds)-64);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.bounces = NO;
    [self.view addSubview:_scrollView];
    
    //加载登陆按钮 45 15 15
    
    for (int i = 0; i < 3; i ++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * CGRectGetWidth(self.view.bounds), 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
        [imageView setImage:[UIImage imageNamed:self.dataSources[i]]];
        imageView.layer.borderWidth = 1;
        [_scrollView addSubview:imageView];
        
        if (i == 0) {
            
            NSLog(@"1");
            _firstImage = [[UIImageView alloc]initWithFrame:CGRectMake(112*PRO, 145*PRO, 96.5*PRO, 96.5*PRO)];
            _firstImage.image = IMGNAME(@"firstLoad.png");
            
            [imageView addSubview:_firstImage];
            
            _firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(55*PRO, 260*PRO, 150, 50)];
            
            _firstLabel.text = @"商户合作,共享双赢";
            _firstLabel.textColor = [UIColor whiteColor];
            _firstLabel.font = BOLD_FONT(25);
            [_firstLabel sizeToFit];
            [imageView addSubview:_firstLabel];
        }
        if (i == 1) {
            NSLog(@"2");
            _SecondImage = [[UIImageView alloc]initWithFrame:CGRectMake(112*PRO, 145*PRO, 96.5*PRO, 96.5*PRO)];
            _SecondImage.image = IMGNAME(@"secondLoad.png");
            
            [imageView addSubview:_SecondImage];
            
            _secondLabel = [[UILabel alloc]initWithFrame:CGRectMake(55*PRO, 260*PRO, 150, 50)];
            
            _secondLabel.text = @"随时上架,随时下架";
            _secondLabel.textColor = [UIColor whiteColor];
            _secondLabel.font = BOLD_FONT(25);
            [_secondLabel sizeToFit];
            [imageView addSubview:_secondLabel];
        }
        if (i == 2) {
            NSLog(@"3");
            _ThredImage = [[UIImageView alloc]initWithFrame:CGRectMake(112*PRO, 145*PRO, 96.5*PRO, 96.5*PRO)];
            _ThredImage.image = IMGNAME(@"thredLoad.png");
            
            [imageView addSubview:_ThredImage];
            
            _thredLabel = [[UILabel alloc]initWithFrame:CGRectMake(55*PRO, 260*PRO, 150, 50)];
            
            _thredLabel.text = @"数据统一,一览无余";
            _thredLabel.textColor = [UIColor whiteColor];
            _thredLabel.font = BOLD_FONT(25);
            [_thredLabel sizeToFit];
            [imageView addSubview:_thredLabel];
        }
        //在imageArray上添加3个imageView
        [self.imageArray addObject:imageView];
    }
    
    _currentIndex = 0;
    //分页控件
    
    // 初始化分页控件
    _pageControl = [[UIPageControl alloc] init];
    _pageControl.pageIndicatorTintColor = [UIColor colorWithHexString:@"000000" alpha:0.7];
    _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.bounds = CGRectMake(0, 0, CGRectGetWidth(_scrollView.bounds), 30*PRO);
    _pageControl.center = CGPointMake(CGRectGetMidX(self.view.bounds),ScreenHeight - 135 * PRO);
    _pageControl.currentPage = _currentIndex;
    _pageControl.numberOfPages = 3;
    [self.view addSubview:_pageControl];
    
    NSArray * btnTitle = @[@"注册",@"登录"];
    for (int i = 0 ; i < 2; i ++) {
        
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.frame = CGRectMake(20, ScreenHeight - 60 * PRO - i * 60 * PRO, ScreenWidth - 40, 45);
        _loginBtn.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
        [_loginBtn setTitle:btnTitle[i] forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = BOLD_FONT(15);
        _loginBtn.layer.cornerRadius = 5;
        _loginBtn.tag = 10+i;
        [_loginBtn addTarget:self action:@selector(processLoginClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_loginBtn];
    }
}
#pragma mark - 重新加载数组里面的图片

- (void)setImageToImageView {
    NSInteger i = 0;
    //因为只有3个所以循环3呲牙
    for (UIImageView *imageView in self.imageArray) {
        //给imageArray 添加改变后的图片（向左或者向右移动一张图片的）
        //遍历，你改变遍历里面的元素，self.imageArray会改变的
        [imageView setImage:[UIImage imageNamed:self.dataSources[i]]];
        i ++;
    }
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger index = scrollView.contentOffset.x/CGRectGetWidth(self.view.bounds);
    _pageControl.currentPage = index;
    
}


#pragma mark-<记录下表>
- (NSInteger)realIndexWithIndex:(NSInteger)index {
    
    // 获取最大索引
    NSInteger maximumIndex = [_imageArray count] - 1;
    // 判断真实索引位置
    if (index > maximumIndex) {
        index = 0;
    }
    else if (index < 0) {
        index = maximumIndex;
    }
    return index;
}


- (void)processLoginClick:(UIButton *)sender
{
    
    switch (sender.tag - 10) {
        case 0:
        {
            NSLog(@"注册");
            //            LoginViewController * loginVc = [[LoginViewController alloc]init];
            //            UINavigationController * navc = [[UINavigationController alloc]initWithRootViewController:loginVc];
            ////             设置viewController动画效果
            //            [loginVc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
            //             [self presentViewController:navc animated:YES completion:nil];
            //            [self.navigationController pushViewController:loginVc animated:YES];
            
//            LoginViewController * regVc = [[LoginViewController alloc]init];
//            UINavigationController * navc = [[UINavigationController alloc]initWithRootViewController:regVc];
//            //            [self presentViewController:navc animated:YES completion:nil];
//            [self.navigationController pushViewController:navc animated:YES];
//            regVc.navigationController.navigationBarHidden = NO;
            
            RegisteredViewController *RegisteredVC = [[RegisteredViewController alloc]init];
            RegisteredVC.index = 11;
            UINavigationController *nav=  [[UINavigationController alloc]initWithRootViewController:RegisteredVC];
            [self presentViewController:nav animated:YES completion:nil];
            
        }
            break;
        case 1:
        {
            NSLog(@"登录");
            //            RegisterViewController * registerVc = [[RegisterViewController alloc]init];
            //               UINavigationController * navc = [[UINavigationController alloc]initWithRootViewController:registerVc];
            //
            //              [self presentViewController:navc animated:YES completion:nil];
            //            [self.navigationController pushViewController:registerVc animated:YES];
            
            
            
            //            FirstViewController * firstVc = [[FirstViewController alloc]init];
            //            UINavigationController * navc = [[UINavigationController alloc]initWithRootViewController:firstVc];
            //            [self presentViewController:navc animated:YES completion:nil];
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            UINavigationController * navc = [[UINavigationController alloc]initWithRootViewController:loginVC];
           [self presentViewController:navc animated:YES completion:nil];
            
            
            
        }
            break;
        default:
            break;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

@end
